package com.gzhryc.shared_device.oem.code.order.services;

import com.gzhryc.common.logging.Logger;
import com.gzhryc.jdbc.Conditions;
import com.gzhryc.jdbc.MultiDBTools;
import com.gzhryc.shared_device.oem.code.order.dao.CDBOrderUserDao;
import com.gzhryc.shared_device.oem.code.order.dao.models.CDBOrderUser;
import com.gzhryc.shared_device.oem.code.order.services.dto.CDBOrderUserSearch;

import java.sql.SQLException;
import java.util.Date;
import java.util.List;

public class CDBOrderUserService {

    static Logger log = Logger.getLogger(CDBOrderUserService.class);

    public static CDBOrderUserService self(Date date) {
        return MultiDBTools.getService(CDBOrderUserService.class, date);
    }

    public static CDBOrderUserService self(String jdbcKey, Date date) {
        return MultiDBTools.getService(jdbcKey, CDBOrderUserService.class, date);
    }

    CDBOrderUserDao dao;

    public CDBOrderUserService(String jdbcKey, Date date) {
        dao = new CDBOrderUserDao(jdbcKey,date);
    }

    public List<CDBOrderUser> findJoinBySearch(CDBOrderUserSearch search, Integer pageNo, Integer pageLimit){
        Conditions conditions = dao.buildConditions(search);
        conditions.and().likeBoth("tradeNo",search.getTrim()).or().likeBoth("placeName",search.getTrim());
        conditions.and().is("deviceOrder.portable_battery_sn",search.getPortableBatterySn());
        conditions.and().in("deviceOrder.state",search.getDeviceOrderStateList());
        conditions.and().is("deviceOrder.state",search.getDeviceOrderState());
        conditions.and().not("deviceOrder.pay_state",search.getNotPayState());
        conditions.and().is("deviceOrder.pay_state",search.getPayState());
        conditions.desc("createDate");
        try {
            return dao.findJoinByCondition(conditions,pageNo,pageLimit);
        } catch (SQLException e) {
            log.error(e.getMessage(),e);
        }
        return null;
    }
}
